﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XLang;

namespace ALang.AST
{
    /// <summary>
    /// 空格
    /// </summary>
    [Ignore]
    public class Space : Token
    {
        public static Space Token(LContext context)
        {
            if (context.CurrentChar != ' ') return null;

            var sb = new StringBuilder();
            while (context.CurrentChar == ' ')
            {
                sb.Append(context.CurrentChar);
                context.GetChar();
            }

            return new Space() { Text = sb.ToString() };
        }
    }

    /// <summary>
    /// 换行符
    /// </summary>
    [Ignore]
    public class CR : Token
    {
        public static CR Token(LContext context)
        {
            //在windows系统下,换行符号是"\r\n",但是在Linux等系统下是"\n"。
            if (context.CurrentChar == '\r' && context.NextChar == '\n')
            {
                context.GetChar();
                context.GetChar();
                return new CR() { Text = "\r\n" };
            }
            else if (context.CurrentChar == '\r')
            {
                context.GetChar();
                return new CR() { Text = "\r" };
            }
            else if (context.CurrentChar == '\n')
            {
                context.GetChar();
                return new CR() { Text = "\n" };
            }

            return null;
        }
    }

    /// <summary>
    /// 文件结尾符
    /// </summary>
    public class EOF : Token
    {
        public static EOF Token(LContext context)
        {
            if (context.CurrentChar != '\0') return null;
            context.GetChar();
            return new EOF() { Text = "EOF" };
        }
    }
}
